package com.zhangshuo.patternDesign.obverser;

import org.omg.CORBA.Object;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by shuo.zhang on 2017-10-22.
 */
public class WriterManager {

    private Map<String,Writer> writers = new HashMap<>();

    private WriterManager(){}

    private static class  WriterManagerInstance{

        private static WriterManager instance = new WriterManager();

    }

    /**
     * 单例
     * @return
     */
    public static WriterManager getInstance(){
        return WriterManagerInstance.instance;
    }

    public boolean addWriter(Writer writer){
        return null == writers.putIfAbsent(writer.getName(),writer);
    }

    public Writer getWriter(String name){
        return writers.get(name);
    }

    public boolean delWriter(String name){
        return !(null == writers.remove(name));
    }

    public Collection<Writer> getAllWriters(){
        return writers.values();
    }
}
